package com.cstz.hb.manage.controller;

import com.cstz.enums.DeptTypeEnum;
import com.cstz.hb.manage.domain.AppUser;
import com.cstz.hb.manage.domain.SysDept;
import com.cstz.hb.manage.service.AppUserService;
import com.cstz.hb.manage.service.SysDeptService;
import com.cstz.ydshop.common.annotation.Log;
import com.cstz.ydshop.common.config.Constant;
import com.cstz.ydshop.common.controller.BaseController;
import com.cstz.ydshop.common.domain.Tree;
import com.cstz.ydshop.common.service.DictService;
import com.cstz.ydshop.common.utils.*;
import com.cstz.ydshop.system.domain.DeptDO;
import com.cstz.ydshop.system.domain.RoleDO;
import com.cstz.ydshop.system.domain.UserDO;
import com.cstz.ydshop.system.service.RoleService;
import com.cstz.ydshop.system.service.UserService;
import com.cstz.ydshop.system.vo.UserVO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/web/user")
@Controller
public class AppUserController extends BaseController {

    private String prefix="hbManage/user"  ;

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private SysDeptService sysDeptService;

	@GetMapping("")
	String user(Model model) {
		return prefix + "/user";
	}

    @Log("添加用户")
    @GetMapping("/add")
    String add(Model model) {
	    List<SysDept> depts = sysDeptService.selectDepts(DeptTypeEnum.组织部.getIndex());
	    model.addAttribute("depts",depts);
        return prefix + "/add";
    }

    @Log("编辑用户")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        AppUser userDO = appUserService.get(id);
        model.addAttribute("user", userDO);
        SysDept sysDept = sysDeptService.getById(userDO.getDeptId());
        model.addAttribute("sysDept",sysDept);
        return prefix+"/edit";
    }


    @GetMapping("/list")
	@ResponseBody
	PageUtils list(@RequestParam Map<String, Object> params) {
		Query query = new Query(params);
		List<AppUser> appUsers = appUserService.list(query);
		int total = appUserService.count(query);
		PageUtils pageUtil = new PageUtils(appUsers, total);
		return pageUtil;
	}

	@Log("保存用户")
	@PostMapping("/save")
	@ResponseBody
	R save(AppUser appUser) {
	    if(StringUtils.isEmpty(appUser.getMobile()) || StringUtils.isEmpty(appUser.getPassword())){
            return R.error("用户名者密码不能为空");
        }
        appUser.setPassword(MD5Utils.encrypt(appUser.getMobile(), appUser.getPassword()));
        appUser.setCreateId(getUserId());
        appUser.setCreateTime(new Date());
        appUser.setModifyTime(new Date());
        if (appUserService.save(appUser) > 0) {
            return R.ok();
        }
        return R.error();
    }

	@Log("更新用户")
	@PostMapping("/update")
	@ResponseBody
	R update(AppUser user) {
        if(StringUtils.isEmpty(user.getMobile()) || StringUtils.isEmpty(user.getPassword())){
            return R.error("用户名者密码不能为空");
        }
        user.setPassword(MD5Utils.encrypt(user.getMobile(), user.getPassword()));
        user.setModifyTime(new Date());
        if (appUserService.update(user) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @Log("删除用户")
    @PostMapping("/remove")
    @ResponseBody
    R remove(Long id) {
        if (appUserService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

	@Log("批量删除用户")
	@PostMapping("/batchRemove")
	@ResponseBody
	R batchRemove(@RequestParam("ids[]") Long[] userIds) {
		int r = appUserService.batchRemove(userIds);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}

    /**
     * 获取部门
     * @param parentId
     * @param deptType
     * @return
     */
	@GetMapping("/getDepts")
    @ResponseBody
	public List<SysDept> getDepts(@RequestParam("deptType")int deptType){
	    List<SysDept> list = sysDeptService.selectDepts(deptType);
        if(DeptTypeEnum.社区.getIndex() == deptType){
            list = sysDeptService.selectDepts(DeptTypeEnum.街道.getIndex());
            if(CollectionUtils.isNotEmpty(list)){
                list.forEach(e->{
                    e.setSubDepts(sysDeptService.selectDeptsByParentId(e.getId()));
                });
            }
        }
        return list;
    }
}
